import {createStore, applyMiddleware, StoreEnhancer, StoreEnhancerStoreCreator, Store, MiddlewareAPI,Dispatch,AnyAction} from '../redux';
import rootReducer, { CombinedState } from './reducers';
import *  as TYPES from '../store/action-types';
//如何定一个专业的中间件
const logger1 = function (api: any) {//api
  return function (next: any) {//下一个中间件
    //以后再调用store.dispatch调用就是这个函数
    return function (action: any) {//派发的动作
      console.log('变更前的老状态1', api.getState());//store.dispatch
      next(action);//调用下一个中间件
      console.log('变更后的新状态1', api.getState());
    }
  }
}
const logger2 = function (api: any) {//api
  return function (next: any) {//下一个中间件
    return function (action: any) {//派发的动作
      console.log('变更前的老状态2', api.getState());//store.dispatch
      next(action);//调用下一个中间件 store.dispatch
      console.log('变更后的新状态2', api.getState());
    }
  }
}
const logger3 = function (api: MiddlewareAPI) {//api
  return function (next: Dispatch<AnyAction>) {//下一个中间件  store.dispatch
    return function (action: any) {//派发的动作
      console.log('变更前的老状态3', api.getState());//store.dispatch
      next(action);//调用下一个中间件
      console.log('变更后的新状态3', api.getState());
    }
  }
}
//如果有中间件的话，创建仓库要经过三步
const storeEnhancer: StoreEnhancer = applyMiddleware<{}, CombinedState>(logger1, logger2, logger3);//返回一个Store的增强器
const storeEnhancerStoreCreator: StoreEnhancerStoreCreator = storeEnhancer(createStore);//返回一个增强后的createStore方法
const store: Store<CombinedState> = storeEnhancerStoreCreator<CombinedState>(rootReducer);//返回仓库
//const store = createStore(rootReducer);
export default store;
